package javao.cn.work.orc.watch;

import javao.cn.work.orc.common.exception.OrcException;
import javao.cn.work.orc.mapper.TbFileInfoMapper;
import javao.cn.work.orc.mapper.TbFileInfoValueMapper;
import javao.cn.work.orc.pojo.TbFileInfo;
import javao.cn.work.orc.pojo.TbFileInfoValue;
import javao.cn.work.orc.pojo.survey.OrcSurvey;
import javao.cn.work.orc.pojo.watch.PathConfig;
import javao.cn.work.orc.service.AF150SurveyService;
import javao.cn.work.orc.service.AF90SurveyService;
import javao.cn.work.orc.utils.pdf.PdfUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author Admin小闫
 * @Date 2024/12/9
 */
@Component
@Slf4j
public class WatchOrcService {

    @Autowired
    private AF90SurveyService af90SurveyService;

    @Autowired
    private AF150SurveyService af150SurveyService;

    @Autowired
    private TbFileInfoMapper tbFileInfoMapper;

    @Autowired
    private TbFileInfoValueMapper tbFileInfoValueMapper;

    @Autowired
    private PdfUtils pdfUtils;


//    @Async("orcTaskExecutor")
    @Transactional
    public void asynORCPdf(Path child, PathConfig pathConfig) throws Exception {
        File file = child.toFile();
        InputStream inputStream=new FileInputStream(file);
        if (pathConfig.getWatchName().equals("AF150")){
            log.debug(file.getName()+"开始转换！");
            Map<Integer, BufferedImage> images = pdfUtils.pdf2png(inputStream);
            inputStream.close();
            OrcSurvey orcSurvey=af150SurveyService.orcSurvey(images);
            System.out.println(file.getName()+"AF150：识别成功");
            insertTable(orcSurvey,file.getName(),pathConfig.getWatchName());
        }else if (pathConfig.getWatchName().equals("AF90")){
            log.debug(file.getName()+"开始转换！");
            Map<Integer, BufferedImage> images = pdfUtils.pdf2png(inputStream);
            inputStream.close();
            OrcSurvey orcSurvey=af90SurveyService.orcSurvey(images);
            System.out.println(file.getName()+"AF90：识别成功");
            insertTable(orcSurvey,file.getName(),pathConfig.getWatchName());
        }else {
            log.debug("未知PATH"+pathConfig.getWatchName());
        }
    }

    public void insertTable(OrcSurvey orcSurvey, String fileName,String watchName) throws Exception {
        TbFileInfo tbFileInfo=new TbFileInfo();
        tbFileInfo.setFileName(fileName);
        tbFileInfo.setFileCode(watchName);
        tbFileInfo.setPartName(orcSurvey.getPartName());
        tbFileInfo.setPartNoKw(orcSurvey.getPartNo());
        tbFileInfo.setPartCounter(orcSurvey.getPartCounter());
        tbFileInfo.setOperator(orcSurvey.getOperator());
        tbFileInfo.setOperatorAf(orcSurvey.getOperator_AF());
        // TODO 测量时间
        tbFileInfo.setMeasureTime(new Date());
        // TODO 数据识别时间
        tbFileInfo.setCreateTime(new Date());

        int insert = tbFileInfoMapper.insert(tbFileInfo);
        log.debug("打印tbFileInfo:"+tbFileInfo.toString());
        List<Map<String, Map<String, Map<String,String>>>> surveyValue = orcSurvey.getSurveyValue();
        for (Map<String, Map<String, Map<String,String>>> stringMapMap : surveyValue) {
            for (Map.Entry<String, Map<String, Map<String,String>>> outerEntry : stringMapMap.entrySet()) {
                String outerKey = outerEntry.getKey();//一连杆
                Map<String, Map<String,String>> innerMap = outerEntry.getValue();
                for (Map.Entry<String, Map<String,String>> innerEntry : innerMap.entrySet()) {
                    String innerKey = innerEntry.getKey();//粗糙读
                    Map<String,String> valueMap = innerEntry.getValue();
                    // 遍历 map 的 entrySet
                    for (Map.Entry<String, String> entry : valueMap.entrySet()) {
                        TbFileInfoValue tbFileInfoValue=new TbFileInfoValue();
                        tbFileInfoValue.setFileId(tbFileInfo.getId());
                        tbFileInfoValue.setConnecting(outerKey);//一连杆
                        tbFileInfoValue.setRoughness(innerKey);//粗糙度
                        String key = entry.getKey();
                        tbFileInfoValue.setProject(key);//项目
                        String value = entry.getValue();
                        tbFileInfoValue.setMeasureValue(value);//值
                        //TODO 测量时间
                        tbFileInfoValue.setMeasureTime(new Date());
                        log.debug("打印tbFileInfoValue:"+tbFileInfoValue.toString());
                        int insert1 = tbFileInfoValueMapper.insert(tbFileInfoValue);
                        if (insert1!=1){
                            throw new OrcException("插入数据失败");
                        }
                        System.out.println("Key: " + key + ", Value: " + value);
                    }
                }
            }

        }
    }

    public void insertTableByF150(OrcSurvey orcSurvey){

    }

}
